package com.xiejr.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @author xiejiarong
 * @since 2020-11-10
 */
@Data
@Accessors(chain = true)
@ApiModel("系统日志")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(value = {"reqParam","respParam"},ignoreUnknown = true)
public class SysLog {

private static final long serialVersionUID=1L;

    @ApiModelProperty("主键")
    @TableId(type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "操作人用户名")
    private String opName;

    @ApiModelProperty(value = "操作者id")
    private Long opId;

    @ApiModelProperty(value = "操作标题")
    private String title;

    @ApiModelProperty(value = "操作资源uri")
    private String uri;

    @ApiModelProperty(value = "来源ip")
    private String ipAddr;

    @ApiModelProperty(value = "浏览器")
    private String browser;

    @ApiModelProperty(value = "操作模型(数据表)")
    private String opModel;


    @ApiModelProperty(value = "请求参数(二进制)")
    private byte[] reqParam;

    @ApiModelProperty(value = "响应参数(二进制)")
    private byte[] respParam;

    @ApiModelProperty(value = "接口执行时间",example = "0")
    private Long execTime;

    @ApiModelProperty(value = "日志水平 0-9,数值越低表示日志重要性越高",example = "0")
    private Integer level;

    @ApiModelProperty(value = "操作类型 INSERT DELETE UPDATE SELECT")
    private String opType;

    @ApiModelProperty(value = "逻辑删除 1：已删除 0：未删除",example = "0")
    private Integer isDelete;

    @ApiModelProperty(hidden = true)
    @TableField(exist = false)
    private SysLogExt ext;

    @ApiModelProperty("请求参数(json字符串)")
    @TableField(exist = false)
    private String reqParamJson;

    @ApiModelProperty("响应参数(json字符串)")
    @TableField(exist = false)
    private String respParamJson;

    @ApiModelProperty("创建时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime ctrTime;

    @ApiModelProperty("更新时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updTime;

    @ApiModelProperty("日志类型 0 系统日志 1 异常日志")
    private Integer logType;


    public void makeLog(){
        if (this.getReqParam()!=null){
            this.setReqParamJson(new String(this.getReqParam(), StandardCharsets.UTF_8));
        }
        if (this.getRespParam()!=null){
            this.setRespParamJson(new String(this.getRespParam(),StandardCharsets.UTF_8));
        }
    }



}
